package com.itheima.service.impl;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.DeptService;
import com.itheima.service.EmpService;
import com.itheima.service.EmptrService;
import jdk.jfr.Threshold;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {
    @Autowired
    private DeptService deptService;
    @Autowired
    private EmptrService empExprService;

    @Override
    public PageResult paginatedQueries(EmpQueryParam param) {
        // 设置分页参数
        Page<Emp> page = Page.of(param.getPage(), param.getPageSize());

        // 分页条件查询
        Page<Emp> page1 = lambdaQuery().like(StrUtil.isNotBlank(param.getName()), Emp::getName, param.getName())
                .eq(ObjectUtil.isNotNull(param.getGender()), Emp::getGender, param.getGender())
                .between(ObjectUtil.isNotNull(param.getBegin()) && ObjectUtil.isNotNull(param.getEnd()),
                        Emp::getEntryDate, param.getBegin(), param.getEnd()).page(page);
        //把查询的所有数据进行遍历
        List<Emp> records = page1.getRecords();
        records.forEach(emp -> {
            Dept dept = deptService.getById(emp.getDeptId());   //根据员工的部门id查询部门名称
            if (dept != null) {
                emp.setDeptName(dept.getName());                //如果部门不为空，则给员工部门赋值
            }
        });
        return new PageResult<>(page1.getTotal(), page1.getRecords());
    }

    @Override
    @Transactional
    public void saveEmp(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        emp.setCreateTime(LocalDateTime.now());
        save(emp);
        List<EmpExpr> exprList = emp.getExprList();
        if (ObjectUtil.isNotEmpty(exprList)) {
            exprList.forEach(e -> {
                e.setEmpId(emp.getId());
            });
            empExprService.saveBatch(exprList);
        }
    }

    @Override
    @Transactional
    public void deleteInBulk(List<Integer> ids) {
        removeByIds(ids);
        empExprService.remove(new QueryWrapper<EmpExpr>().in("emp_id", ids));
    }

    @Override
    public Emp idToQueryTheEmployee(Integer id) {
        //把查询的所有数据赋值给emp
        Emp emp = getById(id);
        if (emp != null) {
            //遍历员工所有数据，对员工经历进行查询，赋值给员工中的字段exprList
            List<EmpExpr> exprList = empExprService.list(new QueryWrapper<EmpExpr>().eq("emp_id", id));
            emp.setExprList(exprList);
        }
        return emp;
    }

    @Override
    @Transactional
    public void modifyEmployeeInformation(Emp emp) {
        // 修改员工信息
        emp.setUpdateTime(LocalDateTime.now());
        //根据id 修改员工信息
        updateById(emp);
        //根据员工id删除员工经历
        empExprService.remove(new QueryWrapper<EmpExpr>().eq("emp_id", emp.getId()));
        //拿到员工经历，如果不为空，遍历员工经历，给员工经历中的员工id赋值成当前员工，再批量保存员工经历
        List<EmpExpr> exprList = emp.getExprList();
        if (ObjectUtil.isNotEmpty(exprList)) {
            exprList.forEach(e -> {
                e.setEmpId(emp.getId());
            });
            empExprService.saveBatch(exprList);
        }
    }
}
